package com.kede.controller;


import com.kede.dto.AjaxMessage;
import com.kede.entity.Agent;
import com.kede.entity.Customer;
import com.kede.entity.User;
import com.kede.service.AgentService;
import com.kede.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 【请填写作者】
 * @since 2020-04-17
 */
@Controller
public class CustomerController {

    @Autowired
    AgentService agentService;

    @Autowired
    CustomerService customerService;

    @GetMapping("/cusdetail")
    public String cusdetail(Integer id,Model model){
        Customer customer = customerService.getById(id);
        model.addAttribute("customer", customer);
        return "customer/cusdetail";
    }

    @GetMapping("/cusinfo")
    public String cusinfo(Integer id,Model model){
        Customer customer = customerService.getById(id);
        model.addAttribute("customer", customer);
        return "customer/cusinfo";
    }

    @ResponseBody
    @PostMapping("/cusinfo")
    public AjaxMessage cusupdate(Customer customer){
        AjaxMessage message = new AjaxMessage();
        if (customerService.updateById(customer)){
            message.setCode(0);
            message.setMsg("更新成功");
        }else {
            message.setCode(1);
            message.setMsg("更新失败");
        }
        return message;
    }
    @ResponseBody
    @PostMapping("/cusdelete")
    public AjaxMessage delete(Integer id){
        AjaxMessage message = new AjaxMessage();
        if(customerService.removeById(id)){
            message.setCode(0);
            message.setMsg("删除成功");
        }else {
            message.setCode(1);
            message.setMsg("删除失败");
        }
        return message;
    }
    @GetMapping("/addcus")
    public String addcus(){
        return "customer/cusadd";
    }

    @ResponseBody
    @PostMapping("customer/cusadd")
    public AjaxMessage addcus(Customer customer){
        AjaxMessage message = new AjaxMessage();
        if (customerService.save(customer)){
            message.setCode(0);
            message.setMsg("添加成功");
        }else {
            message.setCode(1);
        }
        return message;
    }

}
